package com.design_patterns.observer;


import java.util.ArrayList;
import java.util.List;

/**
 * @author: 李德才
 * @description:
 * @create: 2020-08-28 23:02
 **/
public class Main2 {

    public static void main(String[] args) {
        Main2.Child child = new Main2.Child();
        child.setObServe(new Main2.Dad()).setObServe(new Main2.Mum()).setObServe(new Main2.Dog());
        child.weekUp();


    }

    static class WakeUpEvent {
        String addr;
        People people;

        public WakeUpEvent(String addr, Child child) {
            this.addr = addr;
        }

        public String getAddr() {
            return addr;
        }

        public People getPeople() {
            return people;
        }
    }


    static class Dad implements ObServer {
        @Override
        public void actionOnWakeUp(People people) {
            System.err.println("孩子在 " + people.getWakeUpEvent().getAddr());
            System.err.println("给吃的");
        }
    }

    static class Mum implements ObServer {

        @Override
        public void actionOnWakeUp(People people) {
            System.err.println("抱抱");
        }
    }

    static class Dog implements ObServer {

        @Override
        public void actionOnWakeUp(People people) {
            people.demo(this);
        }

        public void wangWang() {
            System.err.println("wang wang wang ");
        }
    }


    static class People {
        private String name;
        private WakeUpEvent wakeUpEvent;

        public WakeUpEvent getWakeUpEvent() {
            return wakeUpEvent;
        }

        public String getName() {
            return name;
        }

        public void demo(Dog dog) {

        }
    }

    static class Child extends People {
        //        我来指定狗狗要干什么
        public void demo(Dog dog) {
            dog.wangWang();
        }

        String name = "Baby";
        private boolean cry = false;
        private List<Main2.ObServer> obServerList = new ArrayList<>();
        private WakeUpEvent wakeUpEvent;

        public String getName() {
            return name;
        }

        public WakeUpEvent getWakeUpEvent() {
            return wakeUpEvent;
        }

        public Child setObServe(Main2.ObServer obServe) {
            this.obServerList.add(obServe);
            return this;
        }

        //        醒了要哭
        public void weekUp() {
            System.err.println(" I'm crying");
            this.wakeUpEvent = new WakeUpEvent("床上", this);
//            处理方法集合
            obServerList.forEach(obServer -> {
                obServer.actionOnWakeUp(this);
            });
        }
    }

    interface ObServer {
        void actionOnWakeUp(People people);
    }
}
